#ifndef __FLASH_H
#define __FLASH_H

#include "system.h"

//0XEF13,表示芯片型号为W25Q80  
//0XEF14,表示芯片型号为W25Q16    
//0XEF15,表示芯片型号为W25Q32  
//0XEF16,表示芯片型号为W25Q64 
//0XEF17,表示芯片型号为W25Q128 

#define FLASH_ID              0XEF17

extern uint8_t flashbuff[4096];

//指令表
#define W25X_WriteEnable		  0x06                                              //写使能
#define W25X_WriteDisable		  0x04                                              //写失能
#define W25X_ReadStatusReg		0x05                                              //读状态寄存器
#define W25X_WriteStatusReg		0x01                                              //写状态寄存器
#define W25X_ReadData			    0x03                                              //读取数据
#define W25X_FastReadData		  0x0B 
#define W25X_FastReadDual	  	0x3B 
#define W25X_PageProgram	  	0x02 
#define W25X_BlockErase		  	0xD8 
#define W25X_SectorErase		  0x20 
#define W25X_ChipErase		  	0xC7 
#define W25X_PowerDown		  	0xB9 
#define W25X_ReleasePowerDown	0xAB 
#define W25X_DeviceID		    	0xAB 
#define W25X_ManufactDeviceID	0x90 
#define W25X_JedecDeviceID		0x9F



#define _SPISSELPORT_		GPIOP
#define _SPISSELPIN_		PIN22

#define SPIFlash_CS_Clr  		GPIO_ClrBit(GPIOP, PIN22)				// SPI片选信号 拉低	
#define SPIFlash_CS_Set  		GPIO_SetBit(GPIOP, PIN22)				// SPI片选信号 置高

#define _SPI0SCLKPORT_		PORTP
#define _SPI0SCLKPIN_			PIN23

#define _SPI0MOSIPORT_		PORTP
#define _SPI0MOSIPIN_			PIN18

#define _SPI0MISOPORT_		PORTP
#define _SPI0MISOPIN_			PIN19




void SPI_Flash_Init(void);
uint8_t SPI_ReadWriteByte(uint8_t data);
uint8_t SPI_ReadSR(void);
void SPI_FLASH_WriteEnable(void);
void SPI_FLASH_WriteDisable(void);
uint16_t SPI_FLASH_ReadID(void);
void SPI_FLASH_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead);
void SPI_FLASH_WritePage(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void SPI_FLASH_WriteNoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void SPI_FLASH_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void SPI_FLASH_EraseSector(uint32_t Dst_Addr);
void SPI_FLASH_WaitBusy(void);
void SPI_FLASH_ChipErase(void);
#endif

